<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-form-item label="奖金类型：" class="width100">
              <el-radio-group
                v-model="tableFrom.bonus_type"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.bonus_type)"
              >
                <el-radio-button
                  v-for="(item,i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                >{{ item.text }}
                </el-radio-button>
              </el-radio-group>

            </el-form-item>
            <el-form-item label="发放状态：" class="width100">
              <el-radio-group
                v-model="tableFrom.status"
                type="button"
                class="mr20"
                size="small"
                @change="selectChangeStatus(tableFrom.status)"
              >
                <el-radio-button
                  v-for="(item,i) in statusList.fromTxt"
                  :key="i"
                  :label="item.val"
                >{{ item.text }}
                </el-radio-button>
              </el-radio-group>

            </el-form-item>

            <el-form-item label="奖金来源：" class="width100">
              <el-radio-group
                v-model="tableFrom.uid_type"
                type="button"
                class="mr20"
                size="small"
                @change="selectUidTpte(tableFrom.uid_type)"
              >
                <el-radio-button
                  v-for="(item,i) in uidList.fromTxt"
                  :key="i"
                  :label="item.val"
                >{{ item.text }}
                </el-radio-button>
              </el-radio-group>

            </el-form-item>
            <el-form-item label="用户搜索：">
              <el-input v-model="tableFrom.keyword" @keyup.enter.native="getList(1)" placeholder="请输入用户手机号" class="selWidth" clearable>
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)" />
              </el-input>
            </el-form-item>
            <el-button size="mini" @click="batch(tableData.data)" class="mr20" style="margin-left: 20px">批量发放</el-button>
            <el-button size="mini" @click="batchAll">一键全发</el-button>
          </el-form>
        </div>
        <cards-data :card-lists="cardLists"/>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        ref="multipleTable"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column label="id" min-width="55">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户名" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.user.nickname }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="number" label="数量" min-width="100"/>
        <el-table-column prop="create_time" label="交易时间" min-width="100" sortable/>

        <el-table-column label="用户类型" min-width="100">
          <template slot-scope="scope">
            <span>{{ getType(scope.row.uid_type) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="奖金类型" min-width="100">
          <template slot-scope="scope">
            <span>{{ getBonusType(scope.row.bonus_type) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否发放" min-width="100">
          <template slot-scope="scope">
            <span>{{ getBonusStatus(scope.row.status) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="备注" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.mark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 1" type="text" size="small"  disabled>发放</el-button>
            <el-button v-else type="text" size="small" @click="grantBonusApi(scope.row.id)">发放</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--导出订单列表-->
    <file-list ref="exportList"/>
    <el-dialog
      title="提示：确定要一键全部发放奖金吗？"
      :visible.sync="show"
      width="600px"

    >

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureDia('form')">提 交</el-button>
        <el-button @click="cancelDia('form')">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {bonuswLstApi, capitalFlowExportApi, getStatisticsApi,grantBonusApi} from '@/api/accounts'
import {bonusList, statuList,uidList} from '@/libs/constants.js'
import fileList from '@/components/exportFile/fileList'
import {roterPre} from '@/settings'
import cardsData from "@/components/cards/index";
import {forEach} from "../../../../public/UEditor/third-party/xss.min";

export default {
  name: 'AccountsBonus',
  components: {fileList, cardsData},
  data() {
    return {
      show:false,
      multipleSelection: [],
      OffId: [],
      timeVal: [],
      tableData: {
        data: [],
        total: 0
      },
      roterPre: roterPre,
      listLoading: true,
      tableFrom: {
        bonus_type: '',
        status: null,
        uid_type: '',
        page: 1,
        limit: 20,
        keyword:''
      },
      bonusFrom:{
        grant_type:1,
        ids:''
      },
      fromList: bonusList,
      statusList: statuList,
      uidList:uidList,
      options: [],
      cardLists: [],
    }
  },
  mounted() {
    this.getList()
    // this.getStatisticalData()
  },
  methods: {
    // 奖金类型选择
    selectChange(tab) {
      this.tableFrom.bonus_type = tab
      this.timeVal = []
      this.tableFrom.page = 1;
      this.getList()
      // this.getStatisticalData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
      const data = []
      this.multipleSelection.map((item) => {
        data.push(item.id)
      })
      this.OffId = data
    },
    //批量发
    // batch() {
    //
    //   if(this.multipleSelection.length === 0) return this.$message.warning('请先选择用户')
    //   this.bonusFrom.grant_type=0
    // },
    batch(rows) {
      if (this.multipleSelection.length === 0) {
        if (rows) {
          rows.forEach(row => {
              if(row.status===0){
                this.$refs.multipleTable.toggleRowSelection(row);
              }
          });
        }
      }
      let ids = ''
        this.multipleSelection.forEach((item, index) => {
            if(index===this.multipleSelection.length-1){
                ids+=item.id
            }else{
                ids+=item.id+','
            }
        });
      console.log(ids)
      this.modalSure(`确定发放奖金给这${this.multipleSelection.length}个用户吗`).then(() => {
          this.grantBonusApi(ids)
        }
      )

    },
    //一键全发
    batchAll() {
      this.show = true;
      // this.grantBonusApi('');
    },
    modalSure(title) {
      return new Promise((resolve, reject) => {
        this.$confirm(`${title || '删除该文件吗'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          resolve()
        }).catch(action => {
          this.$refs.multipleTable.clearSelection();
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      })
    },
    //获取用户类型
    getType(type){
      let s = ''
      if(type==1){
        s='商家'
      }else {
        s='用户'
      }
      return s;
    },
    //奖金类型 1每日释放红包券、2每月月业绩结算、3每月商家直推跟链动二加一业绩结算、4小区业绩结算
    getBonusType(type){
      let s = ''
      switch (type){
        case 1:
          s='每日释放红包券'
          break
        case 2:
          s = '每月月业绩结算'
          break
        case 3:
          s = '每月商家直推跟链动二加一业绩结算'
          break
        case 4:
          s = '小区业绩结算'
          break
      }
      return s;
    },
    getBonusStatus(status){
      let s = ''
      if(status==1){
        s='已发放'
      }else {
        s='未发放'
      }
      return s;
    },
    selectChangeStatus(tab) {
      this.tableFrom.status = tab
      this.timeVal = []
      this.tableFrom.page = 1;
      this.getList()
    },
    selectUidTpte(tab){
      this.tableFrom.uid_type = tab
      this.timeVal = []
      this.tableFrom.page = 1;
      this.getList()
    },

    // 具体日期
    onchangeTime(e) {
      this.timeVal = e
      this.tableFrom.date = e ? this.timeVal.join('-') : ''
      this.tableFrom.page = 1;
      this.getList()
      // this.getStatisticalData()
    },
    //获取统计数据
    getStatisticalData() {
      getStatisticsApi({date: this.tableFrom.date}).then((res) => {
        this.cardLists = res.data;
      })
        .catch((res) => {
          this.$message.error(res.message);
        });
    },
    // 导出
    exportRecord() {
      capitalFlowExportApi(this.tableFrom)
        .then((res) => {
          /*this.$message.success(res.message)
          this.$refs.exportList.exportFileList()*/
          const h = this.$createElement;
          this.$msgbox({
            title: '提示',
            message: h('p', null, [
              h('span', null, '文件正在生成中，请稍后点击"'),
              h('span', {style: 'color: teal'}, '导出记录'),
              h('span', null, '"查看~ '),
            ]),
            confirmButtonText: '我知道了',
          }).then(action => {

          });
        })
        .catch((res) => {
          this.$message.error(res.message)
        })
    },
    // 导出列表
    getExportFileList() {
      this.$refs.exportList.exportFileList()
    },
    // 列表
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num ? num : this.tableFrom.page;
      bonuswLstApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list
          this.tableData.total = res.data.count
          this.listLoading = false
        })
        .catch((res) => {
          this.$message.error(res.message)
          this.listLoading = false
        })
    },
  //单个-多个发放 / 一键全部发放
    grantBonusApi(id){
      this.listLoading = true
      if(id==''){
        this.bonusFrom.grant_type = 0;
      }else{
        this.bonusFrom.ids = id;
        this.bonusFrom.grant_type = 1;
      }
      grantBonusApi(this.bonusFrom)
        .then((res) => {
          this.$message.success(res.message)
          this.listLoading = false
        })
        .catch((res) => {
          this.$message.error(res.message)
          this.listLoading = false
        })
    },

    pageChange(page) {
      this.tableFrom.page = page
      this.getList()
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.selWidth {
  width: 320px;
}
</style>
